const app = getApp()
let Api = app.Api
let store = require('./../../utils/store')
let router = require('./../../utils/router')
Page({
    data: {
        role: '0', // 用户权限
        agree: ''  // 是否点击了条款协议
    },
    onLoad: function () {
        if (store.getItem('isExist')) {
            router.push('company')
        } else {
            this.getSession()
        }
    },
    handleRouteDoc() {
        router.push('xieyi')
    },
    handleCheckBox(e) {
        if (e.detail.value.length) {
            this.setData({
                agree: 'agree',
                isShowTip: false
            })
        } else {
            this.setData({
                agree: ''
            })
        }
    },
    // 普通授权按钮点击事件，用来展示提示信息
    handleBtnClick() {
      if (!this.data.agree) {
          this.setData({
              isShowTip: true
          })
      }
    },
    // 使用手机号进行登录
    loginByPhoneNumber(e) {
        console.log('e', e.detail.code)
        let {
            errMsg,
            iv,
            encryptedData
        } = e.detail
        let session_key = store.getItem('session_key')
        if (errMsg === 'getPhoneNumber:ok') {
            app.post(Api.getPhoneNumber, {
                iv,
                encryptedData,
                sessionKey: session_key
            }).then(async (res) => {
                console.log('login res', res)
                wx.showToast({
                    title: '登录成功'
                })
                store.setItem('phoneNumber', res.phoneNumber)
                store.setItem('isLogin', true)
                await this.getUserExist(res.phoneNumber)
                await this.getUserInfo(res.phoneNumber)
            })
        }
    },
    // 获取用户是否完善个人信息
    async getUserExist(phoneNumber) {
        const res = await app.get(`${Api.getUserExist}?phoneNumber=${phoneNumber}`)
        if (res.exist_code === 0) {
            wx.showModal({
                title: '提示',
                content: '为了给您更好的体验，请您点击确定完善个人信息',
                success (res) {
                    if (res.confirm) {
                        router.push('profile')
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            router.push('company')
        }
    },
     // 获取用户权限
     async getUserInfo(phoneNumber) {
        const res = await app.get(`${Api.getUserInfo}?phoneNumber=${phoneNumber}`)
        console.log('当前用户权限是', res[0].jurisdiction)
        store.setItem('jurisdiction', res[0].jurisdiction)
        const role =  res[0].jurisdiction
        this.setData({
            role
        })
    },
    // 获取session_key和openid
    getSession() {
        wx.login({
            success: (res) => {
                if (res.code) {
                    app.get(Api.getSession, {
                        code: res.code
                    }, {
                        loading: false
                    }).then((res) => {
                        store.setItem('openId', res.openid)
                        store.setItem('session_key', res.session_key)
                    }).catch((err) => {
                        console.log("error: " + err.message)
                    })
                }
            }
        })
    }
});